package jsc.graph;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import jsc.acid.Dictionary;

/**
 * This class display one instance of Dictionnary
 * 
 * @author Alec Ermis, Maxime Rihouey
 *
 */

public class ComponentDictionary extends Container{

	private JTextField field;
	private JButton btnAdd;
	private JButton btnSave;
	private JLabel lblSaved;
	private JLabel lblModified;

	// constructors
	public ComponentDictionary(Dictionary dico){
		initialize(dico);
	}

	// methods
	public void initialize(final Dictionary dico){
		this.setLayout(new GridLayout(5, 1));
		field = new JTextField();
		this.add(field);
		btnAdd = new JButton("Add");
		this.add(btnAdd);
		btnSave = new JButton("Save");
		this.add(btnSave);
		lblSaved = new JLabel("Saved: "+dico.hasBeenSaved());
		this.add(lblSaved);
		lblModified = new JLabel("Modified: "+dico.hasBeenModified());
		this.add(lblModified);

		btnAdd.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String text = field.getText();
				if(text != null){
					dico.add(text);
					field.setText("");
					lblModified.setText(""+true);
					lblSaved.setText(""+false);
				}
			}
		});

		btnSave.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dico.save();
				lblSaved.setText(""+true);
			}
		});
	}
}
